package com.angenin.springcloud.service;

import cn.hutool.core.util.IdUtil;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.concurrent.TimeUnit;

@Service
public class PaymentService {

    //================服务降级==================
    // ================================请求ok======================================
    //正常访问方法
    public String paymentInfo_OK(Integer id) {
        return "线程池：" + Thread.currentThread().getName() + "\tpaymentInfo_OK，id：" + id;
    }


    // ================================服务异常降级====================================
    @HystrixCommand(fallbackMethod = "paymentInfo_ExceptionHandler")
    public String paymentInfo_Exception(Integer id) {
        // todo 这边如果将异常，进行try{...}catch(){}异常捕获，就不会走兜底的降级方法
        int i = 1 / 0;
        return "线程池：" + Thread.currentThread().getName();
    }

    public String paymentInfo_ExceptionHandler(Integer id) {
        return "线程池：" + Thread.currentThread().getName() +
                "\t服务异常，导致服务降级..." + id;
    }

    // ============================服务超时降级========================================
    //超时访问方法    fallbackMethod超时后调用的方法paymentInfo_TimeOutHandler
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",
            commandProperties = {
                    //设置自身超时调用时间的峰值为3秒，峰值内可以正常运行，超过了需要有兜底的方法处理，服务降级fallback
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
            })
    public String paymentInfo_TimeOut(Integer id) {
        int timeNumber = 6;
//        int i = 1 / 0;
        try {
            TimeUnit.SECONDS.sleep(timeNumber);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池：" + Thread.currentThread().getName() +
                "\tpaymentInfo_TimeOut，id：" + id + "，耗时：" + timeNumber + "秒";
    }

    public String paymentInfo_TimeOutHandler(Integer id) {
        return "8001提供者，线程池：" + Thread.currentThread().getName() +
                "\tpaymentInfo_TimeOutHandler系统繁忙，请稍后再试，id：" + id;
    }

    // ============================服务宕机降级========================================
    @HystrixCommand(fallbackMethod = "paymentInfo_downHandler")
    public String paymentInfo_down(Integer id) {

        while (true) {

        }
    }

    public String paymentInfo_downHandler(Integer id) {
        return "线程池：\" + Thread.currentThread().getName()" +
                "\t服务宕机，降级..." + id;
    }
    //=================服务熔断====================

    @HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.enabled", value = "true"),                      //开启断路器
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),         //请求总数阈值（默认20）
                    @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"),   //休眠时间窗口期（休眠多久进入半开模式（单位毫秒，默认5秒））
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"),       //请求次数的错误率达到多少跳闸（百分率%，默认50%）
            })
    public String paymentCircuitBreaker(@PathVariable("id") Integer id) {
        if (id < 0) {
            throw new RuntimeException("****id 不能为负数");
        }
        String serialNumber = IdUtil.simpleUUID();

        return Thread.currentThread().getName() + "\t" + "调用成功，流水号：" + serialNumber;
    }

    public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id) {
        return "服务异常比率达到阈值，服务熔断 ==》id: " + id;
    }

}
